/**
 * 身份证号码验证
 * 
 * @param cardNo
 *            {String} 证件号码
 * @returns info {Object} 身份证信息.
 * 
 */
function idnumber(cardNo) {
  var info = {
		isTrue : false, // 身份证号是否有效。默认为 false
		year : null,// 出生年。默认为null
		month : null,// 出生月。默认为null
		day : null,// 出生日。默认为null
		isMale : false,// 是否为男性。默认false
		isFemale : false // 是否为女性。默认false
	};
	
	if (!cardNo && 15 != cardNo.length && 18 != cardNo.length) {
		info.isTrue = false;
		return info.isTrue;
	}
    var year;
    var month;
    var day;
    var p;
    var birthday;
    if (15 == cardNo.length) {
        year = cardNo.substring(6, 8);
        month = cardNo.substring(8, 10);
        day = cardNo.substring(10, 12);
        p = cardNo.substring(14, 15); // 性别位
        birthday = new Date(year, parseFloat(month) - 1, parseFloat(day)); // 对于老身份证中的年龄则不需考虑千年虫问题而使用getYear()方法
		if (birthday.getYear() != parseFloat(year)
				|| birthday.getMonth() != parseFloat(month) - 1
				|| birthday.getDate() != parseFloat(day)) {
			info.isTrue = false;
		} else {
			info.isTrue = true;
			info.year = birthday.getFullYear();
			info.month = birthday.getMonth() + 1;
			info.day = birthday.getDate();
			if (p % 2 == 0) {
				info.isFemale = true;
				info.isMale = false;
			} else {
				info.isFemale = false;
				info.isMale = true;
			}
		}
		return info.isTrue;
	}
	
	if (18 == cardNo.length) {
	    year = cardNo.substring(6, 10);
	    month = cardNo.substring(10, 12);
	    day = cardNo.substring(12, 14);
	    p = cardNo.substring(14, 17);
	    birthday = new Date(year, parseFloat(month) - 1, parseFloat(day)); // 这里用getFullYear()获取年份，避免千年虫问题
		if (birthday.getFullYear() != parseFloat(year)
				|| birthday.getMonth() != parseFloat(month) - 1
				|| birthday.getDate() != parseFloat(day)) {
			info.isTrue = false;
			return info.isTrue;
		}

		var wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];// 加权因子
		var y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];// 身份证验证位值.10代表X

		// 验证校验位
		var sum = 0; // 声明加权求和变量
		var _cardNo = cardNo.split("");

		if (_cardNo[17].toLowerCase() == 'x') {
			_cardNo[17] = 10;// 将最后位为x的验证码替换为10方便后续操作
		}
	    var i;
	    for ( i = 0; i < 17; i++) {
			sum += wi[i] * _cardNo[i];// 加权求和
		}
	    i = sum % 11; // 得到验证码所位置

		if (_cardNo[17] != y[i]) {
			return info.isTrue = false;
		}

		info.isTrue = true;
		info.year = birthday.getFullYear();
		info.month = birthday.getMonth() + 1;
		info.day = birthday.getDate();
		
		if (p % 2 == 0) {
			info.isFemale = true;
			info.isMale = false;
		} else {
			info.isFemale = false;
			info.isMale = true;
		}
		return info.isTrue;
	}
	return info.isTrue;
}
